/**
 * 微信小程序支付
 * 此处针对于微信小程序开发的支付插件
 * 第一次支付成功后会跳出订阅的消息 如果用户拒绝或同意都会跳转到支付成功页面
 * 如果点击订阅 会将状态写进缓存 之后不再提醒。
 *
 * @param {sn,price}
 */

import { getWeChatMpMessage } from "@/api/message.js";
import clApi from '@/api';
class LiLiWXPay {
  constructor(...payList) {
    this.data = payList[0];
    console.log(payList);
    // 调用支付
    this.pay = () => {
      uni.showLoading({
        title: "加载中",
      });

      let payparam = {
        data : {
          payChannelCode: 'WX',
          payTradeType: 'XCX',
          payBizCode: this.data.payBizCode,
          payAppCode: 'WX-XCX',
          payMchCode: 'WX-TEST',
          amount: '',
          orderId: this.data.orderId,
        }
      }

      const paymentMethod = "WECHAT";

      clApi.order.raisePay(payparam).then(
          (res) => {
            let payData = res.data;
            let payRes = payData.data;
            //如果支付异常
            if(payData.tradeSuccess){
              uni.requestPayment({
                provider: "wxpay",
                appid: payRes.appId,
                timeStamp: payRes.timeStamp,
                nonceStr: payRes.nonceStr,
                package: payRes.package,
                signType: payRes.signType,
                paySign: payRes.paySign,
                success: (e) => {
                  console.log(e);
                  uni.showToast({
                    icon: "none",
                    title: "支付成功!",
                  });
                  sendMessage(0.1)

                },
                fail: (e) => {
                  this.exception = e;
                  // 支付异常或支付失败之后跳转到订单页面
                  uni.showModal({
                    content: "支付失败,如果您已支付，请勿反复支付",
                    showCancel: false,
                    success: () => {
                      uni.redirectTo({
                        url: "/pages/order/myOrder?status=0",
                      });
                    },
                  });
                },
              });
            }else{
              uni.showModal({
                content: payData.errMsg,
                showCancel: false,
                success: res => {
                  uni.hideLoading();
                  uni.navigateBack({
                    delta: 1
                  })
                }
              })
            }
          }
      );
    };
  }
}

function sendMessage(price) {


  //订阅消息
  getWeChatMpMessage().then((res) => {
    var message = res.data.result;
    var templateid = message.map((item) => item.code);
    uni.requestSubscribeMessage({
      tmplIds: templateid,
      success: (res) => {

      },
      fail: (res) => {
        console.log('fail', res)
        uni.showToast({
          icon: "none",
          title: "订阅消息失败",
        })
      },
      complete: (res) => {
        console.log('complete', res)

        /**
         * 已经支付成功
         */
        uni.redirectTo({
          url:
            "/pages/cart/payment/success?paymentMethod=WECHAT" +
            "&payPrice=" +
            price,
        });
      },
    });
  });

}

export default LiLiWXPay;
